#include <iostream>
#include <mutex>
#include <thread>

int shared_data = 0;

std::timed_mutex mtx;

void func() {
    for (int i = 0; i < 2; i++) {
        std::unique_lock<std::timed_mutex> lg(mtx, std::defer_lock); // 不加锁
        if (lg.try_lock_for(std::chrono::seconds(2))) {   // 等待2秒，如果没有锁则加锁，返回true；超过2秒，返回false
            std::this_thread::sleep_for(std::chrono::seconds(1));
            shared_data++;
        }
    }
}

int main() {

    std::thread t1(func);
    std::thread t2(func);

    t1.join();
    t2.join();

    std::cout << shared_data << std::endl;
    return 0;
}